<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>v-model</title>
  </head>
  <body>
    <div id="app">
      <input type="text" v-model="n1" />
      <select v-model="opt">
        <option value="+">加</option>
        <option value="-">减</option>
        <option value="*">乘</option>
        <option value="/">除</option>
      </select>
      <input type="text" v-model="n2" />
      <input type="button" value="=" @click="computed" />
      <input type="text" v-model="n3" />
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
      var vm = new Vue({
        el: "#app",
        data: {
          n1: 0,
          n2: 0,
          n3: 0,
          opt: "+"
        },
        methods: {
          computed() {
            // switch (this.opt) {
            //   case "+":
            //     this.n3 = parseInt(this.n1) + parseInt(this.n2);
            //     break;
            //   case "-":
            //     this.n3 = parseInt(this.n1) - parseInt(this.n2);
            //     break;
            //   case "*":
            //     this.n3 = parseInt(this.n1) * parseInt(this.n2);
            //     break;
            //   case "/":
            //     this.n3 = parseInt(this.n1) / parseInt(this.n2);
            //     break;
            // }

            // 方法二：正式开发中尽量少用
            var codeStr='parseInt(this.n1)' + this.opt+'parseInt(this.n2)'
            this.n3=eval(codeStr)
          }
        }
      });
    </script>
  </body>
</html>
